package org.lifxue.jqda.jintellitype;

import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.SwingUtilities;

import org.lifxue.jqda.run.Command;
import org.lifxue.jqda.ui.QuickStartJFrame;
import org.lifxue.jqda.util.GlobalVar;
import org.lifxue.jqda.util.KeyCode;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * 
 * @Description:
 * @Author: zhangzuoqiang
 * @Date: Sep 26, 2011
 */
public class GlobalHotKeyListener {

	// private static Log logger =
	// LogFactory.getLog(GlobalHotKeyListener.class);

	public static void register() {
		// 注册热键
		JIntellitype.getInstance().registerHotKey(88, JIntellitype.MOD_ALT,
				0x20);// ALT + 空格
		JIntellitype.getInstance().registerHotKey(89, JIntellitype.MOD_ALT,
				(int) 'Z');// ALT + z

		Iterator<Entry<String, String>> i = GlobalVar.HOTKEY.entrySet()
				.iterator();
		while (i.hasNext()) {
			Entry<String, String> entry = (Entry<String, String>) i.next();
			// String key = (String) entry.getKey(); // 返回与此项对应的键
			String mod = (String) entry.getValue(); // 返回与此项对应的值
			String[] tempmods = mod.split(":");
			int id = Integer.parseInt(tempmods[0]);
			int mods = 0;
			for (int le = 1; le < tempmods.length; le++) {
				if (mods != 0) {
					if (tempmods[le].toUpperCase().equals("CTRL")) {
						mods += JIntellitype.MOD_CONTROL;
					} else if (tempmods[le].toUpperCase().equals("ALT")) {
						mods += JIntellitype.MOD_ALT;
					} else if (tempmods[le].toUpperCase().equals("SHIFT")) {
						mods += JIntellitype.MOD_SHIFT;
					}
				} else {
					if (tempmods[le].toUpperCase().equals("CTRL")) {
						mods = JIntellitype.MOD_CONTROL;
					} else if (tempmods[le].toUpperCase().equals("ALT")) {
						mods = JIntellitype.MOD_ALT;
					}
				}
			}

			int keycode = KeyCode.getKeyCode(tempmods[tempmods.length - 1]);
			if (keycode != -1) {
				JIntellitype.getInstance().registerHotKey(id, mods, keycode);
			}

		}
	}

	public static void unregister() {
		JIntellitype.getInstance().unregisterHotKey(88);
		JIntellitype.getInstance().unregisterHotKey(89);
		Iterator<Entry<String, String>> i = GlobalVar.HOTKEY.entrySet()
				.iterator();
		while (i.hasNext()) {
			Entry<String, String> entry = (Entry<String, String>) i.next();
			// String path = (String) entry.getKey(); // 返回与此项对应的键
			String mod = (String) entry.getValue(); // 返回与此项对应的值
			int ind = mod.indexOf(":");
			if (ind > -1) {
				String id = mod.substring(0, ind);
				JIntellitype.getInstance().unregisterHotKey(
						Integer.parseInt(id));
			}
		}

	}

	public static void removerListener() {
		JIntellitype.getInstance().removeHotKeyListener(hkListener);
	}

	public static void addListener() {
		JIntellitype.getInstance().addHotKeyListener(hkListener);
	}

	private static HotkeyListener hkListener = new HotkeyListener() {

		@Override
		public void onHotKey(int key) {
			if (key == 88) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						QuickStartJFrame qs = QuickStartJFrame.getInstance();
						qs.setVisible(true);
					}
				});
			} else if (key == 89) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						QuickStartJFrame qs = QuickStartJFrame.getInstance();
						qs.setVisible(true);
					}
				});
			} else {
				Iterator<Entry<String, String>> i = GlobalVar.HOTKEY.entrySet()
						.iterator();
				while (i.hasNext()) {
					Entry<String, String> entry = (Entry<String, String>) i
							.next();
					String path = (String) entry.getKey(); // 返回与此项对应的键
					String mod = (String) entry.getValue(); // 返回与此项对应的值
					String id = mod.substring(0, mod.indexOf(":"));
					if (key == Integer.parseInt(id)) {
						Command.run(path);
						break;
					}
				}
			}
		}
	};
}
